Improve zsh completion
authorErcan Erden <ercerden@gmail.com>
Sat, 15 Nov 2014 00:17:26 +0000 (19:17 -0500)
committerErcan Erden <ercerden@gmail.com>
Sat, 15 Nov 2014 07:32:47 +0000 (02:32 -0500)
Add test name completion from Cargo.toml

Add package name completion from Cargo.toml

Rename --name to --<target-name>

src/etc/_cargo

index 176ff6e2399d582f85aa0f076425a63df91f429b..2bc520ce553f4ad4c328961df5da782276e5b171 100644 (file)
@@ -1,5 +1,6 @@
 #compdef cargo
 typeset -A opt_args
+autoload -U regexp-replace
 
 _cargo() {
 
@@ -9,22 +10,22 @@ _arguments \
     '(- 1 *)'{-v,--verbose}'[use verbose output]' \
     '(- 1 *)'{-V,--version}'[show version information]' \
     '1: :_cargo_cmds' \
-    '*:: :->args' 
+    '*:: :->args'
 
 case $state in
     args)
         #TODO: add path completion to manifest-path options
-        case $words[1] in 
+        case $words[1] in
             bench)
                 _arguments \
                     '--features=[space separated feature list]' \
                     '(-h, --help)'{-h,--help}'[show help message]' \
                     '(-j, --jobs)'{-j,--jobs}'[number of jobs to run in parallel]' \
                     '--manifest-path=[path to manifest]' \
-                    '--name=[benchmark name]: :_benchmark_names' \
+                    '--bench=[benchmark name]: :_benchmark_names' \
                     '--no-default-features[do not build the default features]' \
                     '--no-run[compile but do not run]' \
-                    '(-p,--package)'{-p=,--package=}'[package to run benchmarks for]:_get_packages' \
+                    '(-p,--package)'{-p=,--package=}'[package to run benchmarks for]:packages:_get_package_names' \
                     '--target=[target triple]' \
                     '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
                     ;;
@@ -36,7 +37,7 @@ case $state in
                     '(-j, --jobs)'{-j,--jobs}'[number of jobs to run in parallel]' \
                     '--manifest-path=[path to manifest]' \
                     '--no-default-features[do not build the default features]' \
-                    '(-p,--package)'{-p=,--package=}'[package to build]:_get_packages' \
+                    '(-p,--package)'{-p=,--package=}'[package to build]:packages:_get_package_names' \
                     '--release=[build in release mode]' \
                     '--target=[target triple]' \
                     '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
@@ -46,7 +47,7 @@ case $state in
                 _arguments \
                     '(-h, --help)'{-h,--help}'[show help message]' \
                     '--manifest-path=[path to manifest]' \
-                    '(-p,--package)'{-p=,--package=}'[package to build]:_get_packages' \
+                    '(-p,--package)'{-p=,--package=}'[package to clean]:packages:_get_package_names' \
                     '--target=[target triple(default:all)]' \
                     '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
                     ;;
@@ -167,26 +168,26 @@ case $state in
                     '(-h, --help)'{-h,--help}'[show help message]' \
                     '(-j, --jobs)'{-j,--jobs}'[number of jobs to run in parallel]' \
                     '--manifest-path=[path to manifest]' \
-                    '--name=[name of the bin target]' \
+                    '--bin=[name of the bin target]' \
                     '--no-default-features[do not build the default features]' \
                     '--release=[build in release mode]' \
                     '--target=[target triple]' \
                     '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
                 ;;
 
-
             test)
                 _arguments \
                     '--features=[space separated feature list]' \
                     '(-h, --help)'{-h,--help}'[show help message]' \
                     '(-j, --jobs)'{-j,--jobs}'[number of jobs to run in parallel]' \
                     '--manifest-path=[path to manifest]' \
-                    '--name=[test name]: :_test_names' \
+                    '--test=[test name]: :_test_names' \
                     '--no-default-features[do not build the default features]' \
                     '--no-run[compile but do not run]' \
-                    '(-p,--package)'{-p=,--package=}'[package to run tests for]:_get_packages' \
+                    '(-p,--package)'{-p=,--package=}'[package to run tests for]:packages:_get_package_names' \
                     '--target=[target triple]' \
                     '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
+                    '1: :_test_names' \
                     ;;
 
             update)
@@ -194,7 +195,7 @@ case $state in
                     '--aggressive=[force dependency update]' \
                     '(-h, --help)'{-h,--help}'[show help message]' \
                     '--manifest-path=[path to manifest]' \
-                    '(-p,--package)'{-p=,--package=}'[package to run tests for]:_get_packages' \
+                    '(-p,--package)'{-p=,--package=}'[package to update]:packages:__get_package_names' \
                     '--precise=[update single dependency to PRECISE]: :_test_names' \
                     '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
                     ;;
@@ -223,7 +224,7 @@ case $state in
                     ;;
         esac
         ;;
-esac 
+esac
 }
 
 _cargo_cmds(){
@@ -257,10 +258,22 @@ _describe 'command' commands
 }
 
 
-#TODO:parse Cargo.toml for package names
-_get_packages(){
-local -a packages;packages=(
-)
+#gets package names from the manifest file
+_get_package_names(){
+local manifest=$(_locate_manifest)
+local -a packages;packages=()
+if ! [[ $manifest ]]; then
+    return 0
+fi
+
+while read line
+do
+    if [[ $line =~ '^.*dependencies' ]]; then
+        regexp-replace line '^.*dependencies\.|\]' ''
+        packages+=$line
+    fi
+    last_line=$line
+done < $manifest
 _describe 'packages' packages
 }
 
@@ -271,10 +284,33 @@ local -a benchmarks;benchmarks=(
 _describe 'tests' tests
 }
 
-#TODO:parse Cargo.toml for test names
+#TODO:see if it makes sense to have 'locate-project' to have non-json output.
+#strips package name from json stuff
+_locate_manifest(){
+local manifest=`cargo locate-project 2>/dev/null`
+regexp-replace manifest '\{"root":"|"\}' ''
+echo $manifest
+}
+
+#gets test names from the manifest file
 _test_names(){
-local -a tests;tests=(
-)
+local -a filelist;
+local manifest=$(_locate_manifest)
+if ! [[ $manifest ]]; then
+    return 0
+fi
+
+local last_line
+local -a tests;
+tests=()
+while read line
+do
+    if [[ $last_line == '[[test]]' ]]; then
+        regexp-replace line '^.*name *= *|"' ""
+        tests+=$line
+    fi
+    last_line=$line
+done < $manifest
 _describe 'tests' tests
 }